home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xvmines-.0 / xvmines- / xvmines-1.0 / play.c < prev    next >
C/C++ Source or Header  |  1993-06-23  |  7KB  |  321 lines

  1. #include <sys/types.h>
  2. #include <sys/timeb.h>
  3. #include <sys/time.h>
  4. #include <X11/Xlib.h>
  5. #include <xview/xview.h>
  6. #include <xview/panel.h>
  7. #include <xview/notify.h>
  8. #include "xvmines.h"
  9.  
  10. #define INITRAND        srandom(time((time_t *)0))
  11. #define MAXRANDOM       2147483648.0    /* 2^31 */
  12. #define RAND01          (((double)(random()))/MAXRANDOM)
  13. #define FRAND(min,max)  (RAND01*((max)-(min))+(min))
  14. #define RAND(min,max)   (int)FRAND(min,max)
  15.  
  16. extern Display *display;
  17. extern GC       def_gc;
  18.  
  19. extern cell    *Cells;
  20. extern int      width, height, first_tile, checks, mines_2_go, closed_tiles,
  21.                 mines, first, playing, quantum, secs;
  22. extern char     buff[];
  23. extern struct itimerval timer;
  24. extern Pixmap   pmap, icons[NUM_ICONS];
  25. extern Window   dr_win;
  26. extern Frame    frame;
  27. extern Panel_item opt;
  28. extern Canvas   canvas;
  29. extern Notify_func active_func;
  30.  
  31. #ifdef __STDC__
  32. static void     settime(Notify_client, int);
  33. #else
  34. static void     settime();
  35. #endif
  36.  
  37.  
  38. #ifdef __STDC__
  39. get_cell(int where_x, int where_y, int *i, int *j)
  40. #else
  41. get_cell(where_x, where_y, i, j)
  42.     int             where_x, where_y, *i, *j;
  43. #endif
  44. {
  45.     *i = where_y / IC_HEIGHT;
  46.     *j = where_x / IC_WIDTH;
  47. }
  48.  
  49. #ifdef __STDC__
  50. place_tiles(int len, int num_mines, int keep_empty)
  51. #else
  52. place_tiles(len, num_mines, keep_empty)
  53.     int             len, num_mines, keep_empty;
  54. #endif
  55. {
  56.     register int    i, j;
  57.     int             where, aux, cnt;
  58.  
  59.     for (i = 0; i < len; i++) {
  60.         (Cells + i)->in_state = EMPTY;
  61.         (Cells + i)->out_state = CLOSED;
  62.     }
  63.     (Cells + keep_empty)->in_state = MINE;
  64.  
  65.     INITRAND;
  66.  
  67.     for (i = 0; i < num_mines;) {
  68.         aux = RAND(0, len);
  69.         if ((Cells + aux)->in_state == MINE)
  70.             continue;
  71.         (Cells + aux)->in_state = MINE;
  72.         i++;
  73.     }
  74.  
  75.     (Cells + keep_empty)->in_state = EMPTY;
  76.  
  77.     for (j = 0; j < width; j++)
  78.         for (i = 0; i < height; i++) {
  79.  
  80.             where = i * width + j;
  81.             if ((Cells + where)->in_state == MINE)
  82.                 continue;
  83.  
  84.             cnt = 0;
  85.             if (i - 1 >= 0) {
  86.                 if ((Cells + where - width)->in_state == MINE)
  87.                     cnt++;
  88.                 if (j - 1 >= 0)
  89.                     if ((Cells + where - width - 1)->in_state == MINE)
  90.                         cnt++;
  91.                 if (j + 1 < width)
  92.                     if ((Cells + where - width + 1)->in_state == MINE)
  93.                         cnt++;
  94.             }
  95.             if (i + 1 < height) {
  96.                 if ((Cells + where + width)->in_state == MINE)
  97.                     cnt++;
  98.                 if (j - 1 >= 0)
  99.                     if ((Cells + where + width - 1)->in_state == MINE)
  100.                         cnt++;
  101.                 if (j + 1 < width)
  102.                     if ((Cells + where + width + 1)->in_state == MINE)
  103.                         cnt++;
  104.  
  105.             }
  106.             if (j - 1 >= 0)
  107.                 if ((Cells + where - 1)->in_state == MINE)
  108.                     cnt++;
  109.  
  110.             if (j + 1 < width)
  111.                 if ((Cells + where + 1)->in_state == MINE)
  112.                     cnt++;
  113.             (Cells + where)->in_state = cnt;
  114.         }
  115.  
  116. #ifdef DEBUG
  117.     for (i = 0; i < height; i++) {
  118.         for (j = 0; j < width; j++)
  119.             if ((Cells + i * width + j)->in_state == MINE)
  120.                 printf("* ");
  121.             else
  122.                 printf("%d ", (Cells + i * width + j)->in_state);
  123.         printf("\n");
  124.     }
  125. #endif
  126.  
  127. }
  128.  
  129.  
  130. #ifdef __STDC__
  131. open_tile(int i, int j)
  132. #else
  133. open_tile(i, j)
  134.     int             i, j;
  135. #endif
  136. {
  137.     cell           *aux;
  138.  
  139.     if (first_tile) {
  140.         notify_set_itimer_func(canvas, (Notify_func) settime, ITIMER_REAL, &timer, NULL);
  141.         active_func = (Notify_func) (settime);
  142.         /* setitimer(ITIMER_REAL,&timer,NULL); */
  143.         first_tile = FALSE;
  144.     }
  145.     aux = Cells + i * width + j;
  146.     if (aux->out_state != CLOSED && aux->out_state != MARKED && aux->out_state != QUEST)
  147.         return FALSE;
  148.  
  149.     if (aux->in_state == MINE) {
  150.         finish("  Game Over");
  151.     } else if (aux->in_state == EMPTY)
  152.         return auto_open(i, j);
  153.     else {
  154.         if (aux->out_state == MARKED) {
  155.             checks--;
  156.             sprintf(buff, "Checks: %d  \n", checks);
  157.             xv_set(frame, FRAME_RIGHT_FOOTER, buff, NULL);
  158.         }
  159.         PLACE_ICON(icons[aux->in_state], i * IC_WIDTH,
  160.                j * IC_HEIGHT);
  161.         aux->out_state = aux->in_state;
  162.         closed_tiles--;
  163.         return TRUE;
  164.     }
  165. }
  166.  
  167. #ifdef __STDC__
  168. mark_tile(int i, int j)
  169. #else
  170. mark_tile(i, j)
  171.     int             i, j;
  172. #endif
  173. {
  174.     cell           *aux;
  175.  
  176.     aux = Cells + i * width + j;
  177.  
  178.     if (aux->out_state == MARKED || aux->out_state != CLOSED && aux->out_state != QUEST)
  179.         return FALSE;
  180.  
  181.     PLACE_ICON(icons[MARKED], i * IC_WIDTH, j * IC_HEIGHT);
  182.     aux->out_state = MARKED;
  183.     checks++;
  184.  
  185.     if (aux->in_state == MINE)
  186.         mines_2_go--;
  187.  
  188.     sprintf(buff, "Checks: %d\n", checks);
  189.     xv_set(frame, FRAME_RIGHT_FOOTER, buff, NULL);
  190.     return TRUE;
  191. }
  192.  
  193. #ifdef __STDC__
  194. auto_open(int i, int j)
  195. #else
  196. auto_open(i, j)
  197.     int             i, j;
  198. #endif
  199. {
  200.     int             where;
  201.     cell           *ptr;
  202.  
  203.     ptr = Cells + i * width + j;
  204.     if (ptr->out_state != CLOSED && ptr->out_state != MARKED && ptr->out_state != QUEST
  205.         || ptr->in_state > EIGHT)
  206.         return FALSE;
  207.  
  208.     if (ptr->out_state == MARKED) {
  209.         checks--;
  210.         sprintf(buff, "Checks: %d\n", checks);
  211.         xv_set(frame, FRAME_RIGHT_FOOTER, buff, NULL);
  212.     }
  213.     PLACE_ICON(icons[ptr->in_state], i * IC_WIDTH, j * IC_HEIGHT);
  214.     ptr->out_state = ptr->in_state;
  215.     closed_tiles--;
  216.  
  217.     if (ptr->in_state == EMPTY) {
  218.         if (i - 1 >= 0) {
  219.             auto_open(i - 1, j);
  220.             if (j - 1 >= 0)
  221.                 auto_open(i - 1, j - 1);
  222.             if (j + 1 < width)
  223.                 auto_open(i - 1, j + 1);
  224.         }
  225.         if (i + 1 < height) {
  226.             auto_open(i + 1, j);
  227.             if (j - 1 >= 0)
  228.                 auto_open(i + 1, j - 1);
  229.             if (j + 1 < width)
  230.                 auto_open(i + 1, j + 1);
  231.         }
  232.         if (j - 1 >= 0)
  233.             auto_open(i, j - 1);
  234.         if (j + 1 < width)
  235.             auto_open(i, j + 1);
  236.  
  237.         return TRUE;
  238.     }
  239. }
  240.  
  241. int
  242. init()
  243. {
  244.     register int    i;
  245.  
  246.     mines_2_go = mines;
  247.     first = playing = TRUE;
  248.     closed_tiles = width * height;
  249.     checks = 0;
  250.     for (i = 0; i < width * height; i++)
  251. #ifndef DEBUG
  252.         PLACE_ICON(icons[CLOSED], (i / width) * IC_WIDTH, (i % width) * IC_HEIGHT);
  253. #else
  254.         PLACE_ICON(icons[(Cells + i)->in_state], (i / width) * IC_WIDTH, (i % width) * IC_HEIGHT);
  255. #endif
  256.  
  257.     XCopyArea(display, pmap, dr_win, def_gc, 0, 0,
  258.           width * IC_WIDTH, height * IC_HEIGHT, 0, 0);
  259.     print_string("Time->> 0:0", TIME_X, TIME_Y, CLEAR_YES);
  260.     xv_set(frame, FRAME_RIGHT_FOOTER, "Checks: 0", NULL);
  261.     secs = 0;
  262.     first_tile = TRUE;
  263.     notify_set_itimer_func(canvas, NOTIFY_FUNC_NULL, ITIMER_REAL, &timer, NULL);
  264.     active_func = NOTIFY_FUNC_NULL;
  265. }
  266.  
  267. #ifdef __STDC__
  268. show_placements(int len)
  269. #else
  270. show_placements(len)
  271.     int             len;
  272. #endif
  273. {
  274.     register int    i;
  275.  
  276.     for (i = 0; i < len; i++)
  277.         if ((Cells + i)->in_state == MINE)
  278.             PLACE_ICON(icons[MINE], (i / width) * IC_WIDTH,
  279.                    (i % width) * IC_HEIGHT);
  280.     XCopyArea(display, pmap, dr_win, def_gc, 0, 0,
  281.           width * IC_WIDTH, height * IC_HEIGHT, 0, 0);
  282. }
  283.  
  284. #ifdef __STDC__
  285. static void
  286. settime(Notify_client client, int which)
  287. #else
  288. static void
  289. settime(client, which)
  290.     Notify_client   client;
  291.     int             which;
  292. #endif
  293. {
  294.     secs += quantum;
  295.     sprintf(buff, "Time->> %2d:%2d", secs / 60, secs % 60);
  296.     print_string(buff, TIME_X, TIME_Y, CLEAR_YES);
  297. }
  298.  
  299. #ifdef __STDC__
  300. void
  301. finish(char *result)
  302. #else
  303. void
  304. finish(result)
  305.     char           *result;
  306. #endif
  307. {
  308.     struct itimerval ovalue;
  309.  
  310.     playing = FALSE;
  311.     show_placements(width * height);
  312.     xv_set(opt, PANEL_INACTIVE, FALSE, NULL);
  313.     /* getitimer(ITIMER_REAL,&timer); */
  314.     notify_set_itimer_func(canvas, NOTIFY_FUNC_NULL, ITIMER_REAL, &timer, &ovalue);
  315.     active_func = NOTIFY_FUNC_NULL;
  316.     secs += quantum - ovalue.it_value.tv_sec;
  317.     sprintf(buff, "Time->> %2d:%2d", secs / 60, secs % 60);
  318.     print_string(buff, TIME_X, TIME_Y, CLEAR_YES);
  319.     print_string(result, RESULT_X, RESULT_Y, CLEAR_NO);
  320. }
  321.